package com.journal.book.service.impl;

import java.util.List;
import com.journal.common.utils.DateUtils;
import com.journal.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.journal.book.mapper.JournalLocationMapper;
import com.journal.book.domain.JournalLocation;
import com.journal.book.service.IJournalLocationService;

/**
 * 期刊定位记录Service业务层处理
 *
 * @author mobin
 * @date 2025-01-XX
 */
@Service
public class JournalLocationServiceImpl implements IJournalLocationService
{
    @Autowired
    private JournalLocationMapper journalLocationMapper;

    /**
     * 记录期刊定位
     * 每次定位都新增一条记录
     * 
     * @param journalCode 期刊编号或名称
     * @param shelfId 书架ID
     * @param sideId 书架侧面
     * @param level 书架层数
     * @param userIdentifier 用户唯一标识（可选）
     * @param ipAddress IP地址（可选）
     * @param location IP所在地区（可选）
     * @param brand 手机品牌（可选）
     * @param model 设备型号（可选）
     * @param osName 操作系统名称（可选）
     * @return 结果
     */
    @Override
    public int recordJournalLocation(String journalCode, String shelfId, String sideId, Long level,
                                     String userIdentifier, String ipAddress, String location,
                                     String brand, String model, String osName)
    {
        if (StringUtils.isEmpty(journalCode))
        {
            return 0;
        }

        // 每次定位都创建新记录
        JournalLocation journalLocation = new JournalLocation();
        journalLocation.setJournalCode(journalCode);
        journalLocation.setShelfId(shelfId);
        journalLocation.setSideId(sideId);
        journalLocation.setLevel(level);
        journalLocation.setLocateTime(DateUtils.getNowDate());
        journalLocation.setUserIdentifier(userIdentifier);
        journalLocation.setIpAddress(ipAddress);
        journalLocation.setLocation(location);
        journalLocation.setBrand(brand);
        journalLocation.setModel(model);
        journalLocation.setOsName(osName);
        journalLocation.setCreateTime(DateUtils.getNowDate());
        
        return journalLocationMapper.insertJournalLocation(journalLocation);
    }

    /**
     * 查询期刊定位记录
     *
     * @param id 期刊定位记录主键
     * @return 期刊定位记录
     */
    @Override
    public JournalLocation selectJournalLocationById(Long id)
    {
        return journalLocationMapper.selectJournalLocationById(id);
    }

    /**
     * 查询期刊定位记录列表
     *
     * @param journalLocation 期刊定位记录
     * @return 期刊定位记录
     */
    @Override
    public List<JournalLocation> selectJournalLocationList(JournalLocation journalLocation)
    {
        return journalLocationMapper.selectJournalLocationList(journalLocation);
    }

    /**
     * 新增期刊定位记录
     *
     * @param journalLocation 期刊定位记录
     * @return 结果
     */
    @Override
    public int insertJournalLocation(JournalLocation journalLocation)
    {
        journalLocation.setCreateTime(DateUtils.getNowDate());
        if (journalLocation.getLocateTime() == null)
        {
            journalLocation.setLocateTime(DateUtils.getNowDate());
        }
        return journalLocationMapper.insertJournalLocation(journalLocation);
    }

    /**
     * 修改期刊定位记录
     *
     * @param journalLocation 期刊定位记录
     * @return 结果
     */
    @Override
    public int updateJournalLocation(JournalLocation journalLocation)
    {
        journalLocation.setUpdateTime(DateUtils.getNowDate());
        return journalLocationMapper.updateJournalLocation(journalLocation);
    }

    /**
     * 批量删除期刊定位记录
     *
     * @param ids 需要删除的期刊定位记录主键
     * @return 结果
     */
    @Override
    public int deleteJournalLocationByIds(Long[] ids)
    {
        return journalLocationMapper.deleteJournalLocationByIds(ids);
    }

    /**
     * 删除期刊定位记录信息
     *
     * @param id 期刊定位记录主键
     * @return 结果
     */
    @Override
    public int deleteJournalLocationById(Long id)
    {
        return journalLocationMapper.deleteJournalLocationById(id);
    }
}

